package com.example.wifijailbreak.thread;

import cn.hutool.core.io.FileUtil;
import com.example.wifijailbreak.config.WifiProfile;
import org.apache.commons.lang3.StringUtils;

import java.io.File;
import java.nio.charset.StandardCharsets;
import java.util.concurrent.CountDownLatch;

public class GenThread implements Runnable {

    private final String ssid;
    private final String password;
    private final String profileName;
    private final CountDownLatch latch;

    public GenThread(String ssid, String password, String profileName, CountDownLatch latch) {
        this.ssid = ssid;
        this.password = password;
        this.profileName = profileName;
        this.latch = latch;
    }

    @Override
    public void run() {
        String wifiProfileContent = StringUtils.replaceEach(WifiProfile.PROFILE,
                new String[]{WifiProfile.WIFI_NAME, WifiProfile.PASSWORD},
                new String[]{ssid, password});
        File pfoFile = FileUtil.newFile(WifiProfile.WIFI_PROFILE_PATH + profileName);
        FileUtil.writeString(wifiProfileContent, pfoFile, StandardCharsets.UTF_8);
        latch.countDown();
    }
}
